/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.follow.FollowUpItemRecord;

public interface FollowUpItemRecordDao extends PagingAndSortingRepository<FollowUpItemRecord, Long>, JpaSpecificationExecutor<FollowUpItemRecord> {

	/**
	 * 根据计划code查找出随访项的每个周期
	 * 
	 * @param code
	 * @return
	 */
	@Query("select a from FollowUpItemRecord a where a.plan = ?1 order by a.czrq desc")
	List<FollowUpItemRecord> findByPlanCode(String code);

	@Query("select a from FollowUpItemRecord a where a.code = ?1")
	FollowUpItemRecord getFollowUpItemRecordByCode(String recoredCode);

	List<FollowUpItemRecord> findByItemOrderByCzrqDesc(String item);

	FollowUpItemRecord findByCode(String code);

	List<FollowUpItemRecord> findByTitle(String queryParam);

	@Query("select a from FollowUpItemRecord a where  a.title=?1 order by a.czrq desc")
	List<FollowUpItemRecord> getFollowUpPlanStageItemListByTitle(String title, String userid);

	@Query("select a from FollowUpItemRecord a where a.plan = ?1  order by a.czrq desc")
	List<FollowUpItemRecord> getFollowUpPlanStageItemListByPlanCode(String planCode);

	int countByItem(String item);

	@Query("select a from FollowUpItemRecord a where a.plan = ?1 and a.type=?2 order by a.czrq desc")
	List<FollowUpItemRecord> getFollowUpPlanStageItemListByPlanCodeAndType(String planCode, String type);

	@Query("select distinct(a.item) from FollowUpItemRecord a where a.plan = ?1 and a.type=?2 order by a.czrq desc")
	List<String> findByPlanCodeAndTypeDistinctItemCode(String planCode, int type);

}
